package com.xxxx;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;

/**
 * 批量数据处理
 */
public class Hello15Flink {
    public static void main(String[] args) throws Exception {
        //创建环境
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        //获取数据源
        DataStreamSource<String> source = environment.socketTextStream("192.168.88.101", 18880);
        //对流进行处理
        source.flatMap((String line, Collector<Tuple2<String, Integer>> collector) -> {
            Arrays.stream(line.split(" ")).forEach(word -> collector.collect(Tuple2.of(word, 1)));
        }).returns(Types.TUPLE(Types.STRING, Types.INT)).keyBy(0).sum(1).print();

        //执行流
        environment.execute("Hello15Flink" + System.currentTimeMillis());

    }
}
